package net.csdn.business.notify.handler.action;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import net.csdn.business.notify.common.enums.NotifyChannel;
import net.csdn.business.notify.common.model.entity.NotifyRemindRecord;
import net.csdn.business.notify.common.pipeline.BusinessProcess;
import net.csdn.business.notify.common.pipeline.ProcessContext;
import net.csdn.business.notify.common.pipeline.domain.TaskInfo;
import net.csdn.business.notify.handler.service.INotifyRemindRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;



/**
 * @ClassName SaveDirectAction
 * @Description 保存直达类消息通知记录
 * @Author zhangyl
 * @Date 2023/12/20 14:57
 */
@Slf4j
@Service
public class SaveDirectAction implements BusinessProcess<TaskInfo> {

    @Autowired
    private INotifyRemindRecordService remindRecordService;

    @Override
    public void process(ProcessContext<TaskInfo> context) {
        TaskInfo taskInfo = context.getProcessModel();
        List<String> receivers=new ArrayList<>(taskInfo.getReceiver());
        Integer notifyChannel=taskInfo.getNotifyChannel();
        Integer notifyType=taskInfo.getNotifyType();
        //站内信通知渠道不允许投递直达消息
        if(NotifyChannel.IM.getCode().equals(notifyChannel)){
            //如果邮件接收账号为空，则终止流程
            context.setNeedBreak(true);
            return;
        }
        String senderId=taskInfo.getSenderId();
        String businessCode= taskInfo.getBusinessCode();
        JSONObject contentJson=JSON.parseObject(taskInfo.getContentModel());
        String content=contentJson.getString("content");
        String batchNum=taskInfo.getBatchNum();
        //批量查询用户信息，并记录到数据库
        if (receivers!=null&&receivers.size()>0){
            //防止查询数据过多，分批查询
            List<String> receiversAccount=new ArrayList<>();
            List<List<String>> uidBathList= Lists.partition(receivers, 50);
            for (List<String> userIdList:uidBathList){
                    //批量入库当前批次通知记录
                    List<NotifyRemindRecord> records=new ArrayList<>();
                    for (String userId:userIdList){
                        if(StringUtils.isBlank(userId)){
                            continue;
                        }
                        NotifyRemindRecord remindRecord=new NotifyRemindRecord();
                        remindRecord.setObjectId(IdUtil.fastSimpleUUID());
                        remindRecord.setBatchNum(batchNum);
                        remindRecord.setBusinessCode(businessCode);
                        remindRecord.setSenderId(senderId);
                        //remindRecord.setReceiverId(userId);
                        remindRecord.setReceiverAccount(userId);
                        remindRecord.setNotifyChannel(notifyChannel);
                        remindRecord.setNotifyType(notifyType);
                        remindRecord.setContent(content);
                        remindRecord.setNotifyState(0);
                        remindRecord.setReadState(0);
                        //当用户存在时,组装用户通知记录信息，批量保存入库
                        remindRecord.setCreatedDate(taskInfo.getSendDate());
                        records.add(remindRecord);
                        receiversAccount.add(userId);
                    }
                    if(records.size()>0){
                        //此处后面可以改为异步
                        remindRecordService.insertBatch(records);
                    }
                }
            if(notifyChannel.equals(NotifyChannel.EMAIL.getCode())&&receiversAccount.size()==0){
                //如果邮件接收账号为空，则终止流程
                context.setNeedBreak(true);
            }
            taskInfo.setAccounts(receiversAccount);
        }
    }
}
